package com.join.template.html.grammar;

import com.join.template.core.Template;
import com.join.template.core.constant.Constant;
import com.join.template.core.constant.EntityType;
import com.join.template.core.context.HashContext;
import com.join.template.core.factory.template.TemplateFactory;
import com.join.template.core.grammar.info.FieldGenerateInfo;
import com.join.template.core.grammar.info.GrammarInfo;
import com.join.template.core.grammar.tool.AbstractTool;
import com.join.template.core.grammar.tool.GrammarTool;
import com.join.template.core.util.Assert;
import com.join.template.core.util.RandomUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class HtmlGrammarTool extends AbstractTool implements GrammarTool {


    @Override
    public String preview(String text, int previewSize) {
        Assert.isNull(generate, "未创建生成器");

        Map<String, Object> map = new HashMap<>();
        List<GrammarInfo> grammarInfos = generate.getGrammarInfos();
        for (int i = 0; i < grammarInfos.size(); i++) {
            GrammarInfo grammarInfo = grammarInfos.get(i);
            genContent(grammarInfo, map, previewSize);
        }
        TemplateFactory templateFactory = joinFactory.getTemplateFactorys(Constant.TYPE_SINGLE);
        String name = System.currentTimeMillis() + "preview.html";
        Template template = templateFactory.putTemplate(name, text);
        template.putContext(new HashContext(map));
        return template.process();
    }


    private void genContent(GrammarInfo grammarInfo, Map<String, Object> map, int previewSize) {
        Object value = null;
        List<GrammarInfo> childs = grammarInfo.getChilds();
//        if (EntityType.Integer == grammarInfo.getType()) {
//            value = RandomUtil.toInt(0, 999999999);
//        } else if (EntityType.Byte == grammarInfo.getType()) {
//            value = RandomUtil.toByte();
//        } else if (EntityType.Long == grammarInfo.getType()) {
//            value = RandomUtil.toLong();
//        } else if (EntityType.Float == grammarInfo.getType()) {
//            value = RandomUtil.toFloat();
//        } else if (EntityType.BigDecimal == grammarInfo.getType()) {
//            value = RandomUtil.toBigDecimal();
//        } else if (EntityType.BigInteger == grammarInfo.getType()) {
//            value = RandomUtil.toBigInteger();
//        } else if (EntityType.Short == grammarInfo.getType()) {
//            value = RandomUtil.toShort(3);
//        } else if (EntityType.Character == grammarInfo.getType()) {
//            value = RandomUtil.toCharacter();
//        } else if (EntityType.Time == grammarInfo.getType()) {
//            value = RandomUtil.toDouble();
//        } else if (EntityType.Double == grammarInfo.getType()) {
//            value = RandomUtil.toDouble();
//        } else if (EntityType.Boolean == grammarInfo.getType()) {
//            value = RandomUtil.toBoolean();
//        } else if (EntityType.Object == grammarInfo.getType()) {
//            value = RandomUtil.toString(6);
//        } else if (EntityType.String == grammarInfo.getType()) {
//            value = RandomUtil.toString(6);
//        } else
//
        if ((EntityType.Array == grammarInfo.getType() || EntityType.Entity == grammarInfo.getType()) && childs != null) {
            Map<String, Object> childMap = new HashMap<>();
            for (int i = 0; i < childs.size(); i++) {
                GrammarInfo child = childs.get(i);
                genContent(child, childMap, previewSize);
            }
            if (EntityType.Array == grammarInfo.getType()) {
                List<Map<String, Object>> list = new ArrayList<>();
                for (int i = 0; i < previewSize; i++) {
                    list.add(childMap);
                }
                value = list;
            } else {
                value = childMap;
            }
        } else {
            value = "XXXX";
        }
        if (value != null) {
            map.put(grammarInfo.getName(), value);
            if (this.generateListener != null) {
                this.generateListener.onPreview(grammarInfo, map, generateConfig);
            }
        }
    }


}
